<template>
	<view class="main">
		<view class="selfInfo">
			<view class="selfInfo_left">
				<view class="headPic">
					<image :src="userInfo.avatar" mode="widthFix"></image>
				</view>
				<view class="headInfo">
					<view class="name" v-if="userInfo.nickName">{{userInfo.nickName}}</view>
					<view class="name" v-if="!userInfo.nickName">暂未授权</view>
					<view class="tel" v-if="phoneNumber">{{phoneNumber}}</view>
				</view>
			</view>
			<view class="kf" @click="call">
				<image src="../../static/my/kf.png" mode="widthFix"></image>
				<view>客服</view>
			</view>
		</view>
		<view class="myCar">
			<view class="myCartit">
				<view>
					<image src="../../static/my/mcar.png" mode="widthFix"></image>
					<text>我的车辆</text>
				</view>
				<!-- 需判断是否存在车辆，若不存在不显示 -->
				<view class="lookMore" v-if="isHasCar">
					查看所有
				</view>
			</view>
			<!-- 无车辆时的添加按钮 -->
			<view class="myCarCon" v-if="!isHasCar" @click="addCar">
				<image src="../../static/my/tj.png" class="add"></image>添加车辆
			</view>
			<!-- 添加车辆之后 -->
			<view class="myCarCon01" v-if="isHasCar">
				<view class="carNum">
					<text class="number">{{currentUserCarInfo.Carno}}</text>
					<text class="carsize">{{currentUserCarInfo.pinpai}}</text>
				</view>
				<view class="edit" @click="toEditCar">编辑</view>
			</view>
		</view>
		<view class="selfCon">
			<!-- 我的卡券 -->
			<view class="myCard">
				<view class="myCartit myCardtit">
					<view>
						<image src="../../static/my/kq.png" mode="widthFix"></image>
						<text>待核销卡券</text>
					</view>
					<!-- 需判断是否存在车辆，若不存在不显示 -->
					<!-- <view class="buyNow" @click="toBuy">
						立即购买
					</view> -->
				</view>
				<!-- <view class="cardStatus">
					<view :class="[isChoose01?'selected':'']" @click="choose01">待使用</view>
					<view :class="[isChoose02?'selected':'']" @click="choose02">待评价</view>
				</view> -->
				<!-- 待使用 -->
				<!-- <view v-if="isChoose01"> -->
				<view v-if="waitList!=''">
					<view class="cardList" v-for="item in waitList">
						<view class="price"><text>￥</text>{{item.item_price}}</view>
						<view class="cardDetail">
							<view class="cd_top">
								<view class="productInfo">
									<view class="productName uni-ellipsis">{{item.item_name}}</view>
									<view class="productNum">{{item.item_num}}</view>
								</view>
								<view class="checkCard" @click="goHx(item.customer_item_id)">
									立即核销
								</view>
							</view>
							<view class="cd_btm">
								<view class="cardDate">
									{{item.item_riqi}}
								</view>
								<view class="cardFitStore" @click="cardFitStore(item.customer_item_id)">
									适用门店
								</view>
							</view>
						</view>
					</view>
					<view class="buyNow" @click="toBuy">
						立即购买
					</view>
				</view>
				<view class="myCartit myCardtit">
					<view>
						<image src="../../static/my/kq01.png" mode="widthFix"></image>
						<text>待评价卡券</text>
					</view>
				</view>
				<view v-if="doneList!=''">
					<!-- 待评价 -->
					<!-- <view v-if="isChoose02" class="waitComment"> -->
					<view class="waitComment">
						<view class="cardList" v-for="item in doneList">
							<view class="price"><text>￥</text>{{item.item_price}}</view>
							<view class="cardDetail">
								<view class="cd_top">
									<view class="productInfo">
										<view class="productName uni-ellipsis">{{item.item_name}}</view>
										<view class="productNum">{{item.customer_item_id}}</view>
									</view>
									<view class="checkCard" @click="toPl(item.customer_item_id)">
										立即评价
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				
				
			</view>
		</view>
		<view class="modal" v-if="noCode || noPhone" @click="hideModal"></view>
		<view class="power" v-if="noCode || noPhone">
			<button class="sq" open-type="getAuthorize"@getAuthorize="onalipaySignin" v-if="noCode"
			 onError="onAuthError" 
			scope='userInfo'>
			    会员基础信息授权
			</button>
			<!-- <button class="getPhone" type="default" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">获取手机号</button> -->
		</view>
	</view>
	
	
	
</template>

<script>
	export default {
		data() {
			return {
				clientList:[],
				isChoose01:true,
				isChoose02:false,
				isHasCar:false,
				showSQ:true,
				userInfo:{},
				waitList:[],
				doneList:[],
				currentUserCarInfo:[],
				nowLongitude:'119.950218',
				nowLatitude:'31.835252',
				phoneNumber:"",
				noCode:true,
				noPhone:false,
		}
	},
		// 	}
		// },
		components: {

		},
		onShow() {
			console.log("onShow")
			this.getCurrentUserCarInfo();
			//获取缓存中的userInfo和手机号码
			this.userInfo = uni.getStorageSync('userInfo');
			var userId = uni.getStorageSync('userId');
			var phone = uni.getStorageSync('phone');
			this.noCode = !userId;
			this.phoneNumber = uni.getStorageSync('phoneNumber');
			this.getWaitList()
			this.getDoneList()
		},
		onPullDownRefresh() {
			this.getCurrentUserCarInfo();
		},
		onload() {
			console.log("onload");
			this.getWaitList();
			this.getDoneList();
			// this.getCurrentUserCarInfo();
		},
		methods: {
			call(){
				uni.makePhoneCall({
				    phoneNumber: "0519-8119-0919" 
				});
			},
			toBuy(){
				uni.switchTab({
					url:'/pages/index'
				})
			},
			cardFitStore(id){
				uni.navigateTo({
					url:'/pages/suitableShop?id='+id
				})
			},
			toEditCar(){
				uni.navigateTo({
					url:'/pages/addCar/addCar'
				})
			},
			//评论
			toPl(id){
				uni.navigateTo({
					url:'/pages/comment/comment?id='+id
				})
			},
			getDoneList(){
				this.$get('customer_item_1.ashx',{
					id:uni.getStorageSync('userId')
				},(res)=>{
					if(res.data!=4){
						this.doneList = res.data
					}
				})
			},
			//核销
			goHx(id){
				uni.navigateTo({
					url:'/pages/checkOrder/checkOrder?id='+id
				})
			},
			//等待核销的项目列表
			getWaitList(){
				this.$get('customer_item.ashx',{
					id:uni.getStorageSync('userId')
				},(res)=>{
					// console.log(res.data);
					if(res.data!=4){
						this.waitList = res.data
					}
				})
			},
			// 添加车辆
			addCar() {
				uni.navigateTo({
					url:'/pages/carList/carList'
				})
			},
			// 获取当前用户的车辆信息
			getCurrentUserCarInfo() {
				this.$get('customer_auto.ashx',{
					id:uni.getStorageSync('userId')
				},(res)=>{
					if(res.data.length > 0) {
						this.currentUserCarInfo = res.data[0];
						this.isHasCar = true
					} else {
						this.isHasCar = false
					}
				})
			},
			choose01(){
				this.isChoose01 = true;
				this.isChoose02 = false;
			},
			choose02(){
				this.isChoose01 = false;
				this.isChoose02 = true;
			},
			onalipaySignin(res) {
				var that = this;
				uni.login({
					provider: 'alipay',
					success: function(loginRes) {
						console.log(loginRes); // 获取用户信息 
						uni.getUserInfo({
							provider: 'alipay',
							withCredentials:true,  
							success: (res)=> {
								 let userInfo = JSON.parse(res.response).response;
								 uni.setStorageSync('userInfo', JSON.parse(res.response).response);
								 that.register(loginRes.authCode)
								 that.noCode = false;
								 that.userInfo = userInfo;
								 that.getWaitList();
								 that.getDoneList();
								  console.log(userInfo)
							}
						});
					}
				});
			
			},
			register(authCode){
				this.$get('api_customer_register.ashx',{
					id:authCode,
					Pic:this.userInfo.avatar,
					// Tel:this.userInfo.Tel,
					City:this.userInfo.city,
					Name:this.userInfo.nickName,
					// Tel:""
					},(res)=>{
					console.log(res)
				})
			},
			decryptPhoneNumber(){
				this.$get('customer_info.ashx',{
					id:uni.getStorageSync('authCode')
					},(res)=>{
					console.log(res)
				})
			},
			//隐藏授权弹窗
			hideModal() {
				var that = this;
				uni.showModal({
					title:'提示',
					content:'若不授权则无法使用全部功能！',
					confirmText:'确定',
					cancelText:'取消',
					success(res) {
						if (res.confirm) {
							that.noCode = false;
							that.noPhone = false;
						} else if (res.cancel) {
							that.noCode = true;
							that.noPhone = true;
						}
						
					},
					fail() {
						
					},
					complete() {
						
					}
				})
				
			}
			
		
	},
	}
</script>

<style>
	.main {
		width: 750rpx;
		margin-bottom: 100rpx;
	}
	.selfInfo {
		width: 750rpx;
		height: 260rpx;
		background: url(../../static/my/bg.png) no-repeat;
		background-size: 100% 100%;
		display: flex;
		justify-content: flex-start;
		align-items: center;
	}
	.headPic image {
		width: 126rpx;
		height: 126rpx;
		vertical-align: middle;
		border-radius: 50%;
	}
	.kf image {
		width: 54rpx;
		height: 50rpx;
	}
	.selfInfo_left {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		width: 620rpx;
		margin-left: 24rpx;
	}
	.headInfo {
		margin-left: 24rpx;
	}
	.name {
		font-size: 48rpx;
		color: #fff;
	}
	.tel {
		color: #fff;
		font-size: 24rpx;
	}
	.kf view {
		font-size: 24rpx;
		color: #fff;
	}
	.selfCon {
		width: 750rpx;
		background: #fff;
		padding: 10rpx 24rpx;
		box-sizing: border-box;
		margin-top: 48rpx;
	
	}
	.myCar {
		padding: 0 24rpx;
		box-sizing: border-box;
		background: #fff;
		border-radius: 18rpx;
		width: 702rpx;
		margin: -32rpx auto;
	}
	.myCartit {
		display: flex;
		justify-content: space-between;
		padding-top: 14rpx;
	}
	.myCartit view image {
		width: 32rpx;
		height: 26rpx;
		margin-right: 13rpx;
		vertical-align: middle;
	}
	.myCartit view text {
		color: #333;
		font-size: 28rpx;
		font-weight: bold;
		letter-spacing: 2rpx;
	}
	.lookMore {
		color: #999;
		font-size: 22rpx;
		margin: 0 15rpx 0 0; 
	}
	.myCarCon {
		text-align: center;
		color: #1677ff;
	}
	.myCarCon .add {
		width: 40rpx;
		height: 40rpx;
		/* margin: 0 auto; */
		vertical-align: middle;
		margin-right: 17rpx;
	}
	.myCarCon01 {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.myCardtit {
		padding: 0 24rpx;
		margin-bottom: 40rpx;
	}
	.number {
		font-size: 36rpx;
		color: #333;
		font-weight: bold;
		margin-left: 44rpx;
	}
	.carsize {
		color: #999;
		font-size: 24rpx;
		margin-left: 24rpx;
	}
	.edit {
		width: 90rpx;
		height: 34rpx;
		line-height: 34rpx;
		background: #cfe3ff;
		font-size: 22rpx;
		color: #2d80ff;
		border-radius: 20rpx;
		text-align: center;
	}
	.buyNow {
		width: 300rpx;
		height: 80rpx;
		background: #fe9a43;
		color: #fff;
		font-size: 32rpx;
		border-radius: 40rpx;
		text-align: center;
		line-height: 80rpx;
		margin: 47rpx auto 50rpx; 
	}
	.myCard {
		margin-top: 60rpx;
	}
	.cardStatus {
		display: flex;
		justify-content: space-around;
		align-items: center;
	}
	.cardStatus view {
		color: #999;
		font-size: 22rpx;
		margin-top: 50rpx;
		margin-bottom: 40rpx;
	}
	.cardStatus view.selected {
		border-bottom: 2rpx solid #999;
	}
	.cardList {
		border: 1rpx solid #eee;
		border-radius: 30rpx;
		/* padding: 0 24rpx; */
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-shadow: 0 3rpx 5rpx #eeeeee;
		margin-bottom: 20rpx;
	}
	.price {
		width: 162rpx;
		height: 260rpx;
		line-height: 260rpx;
		text-align: center;
		background: linear-gradient(#fcc698,#fdb97f,#fe963b);
		font-size: 52rpx;
		color: #fff;
		font-weight: bold;
		border-top-left-radius: 30rpx;
		border-bottom-left-radius: 30rpx;
	}
	.price text {
		font-weight: normal;
		font-size: 38rpx;
	}
	.cardDetail {
		width: 490rpx;
	}
	.productName {
		font-size: 28rpx;
		color: #333;
	}
	.productNum {
		font-size: 26rpx;
		letter-spacing: 2rpx;
		color: #999;
	}
	.cd_top {
		display: flex;
		justify-content: space-around;
		align-items: center;
		padding-bottom: 30rpx;
		padding-top: 44rpx;
		border-bottom: 2rpx dashed #999999;
	}
	.checkCard {
		width: 120rpx;
		height: 44rpx;
		line-height: 44rpx;
		border: 1rpx solid #fd7878;
		border-radius: 20rpx;
		color: #fd7878;
		font-size: 22rpx;
		text-align: center;
		margin-right: 16rpx;
	}
	.productInfo {
		width: 363rpx;
		
	}
	.cd_btm {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding-top: 25rpx;
		padding-bottom: 25rpx;
	}
	.cardDate {
		font-size: 20rpx;
		color: #ff8f2d;
	}
	.cardFitStore {
		font-size: 22rpx;
		color: #999999;
		margin-right: 35rpx;
	}
	.waitComment .price { 
		background: linear-gradient(#c5c6c6,#bfc0bf,#9e9f9e);
		color: #fff;
	}
	.waitComment .cd_top {
		border-bottom: none;
	}
	.modal {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 100rpx;
		background: rgba(51,51,51,0.4);
		z-index: 1;
	}
	.power {
		width: 750rpx;
		height: 302rpx;
		background: #fff;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 2;
	}
	.power button.sq {
		width: 670rpx;
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		font-size: 36rpx;
		color: #fff;
		background: #008afd;
		border-radius: 40rpx;
		margin: 82rpx auto 34rpx;
		
	}
	.getPhone {
		color: rgb(51,51,51);
		font-size: 36rpx;
		text-align: center;
		border: none;
		background: none;
	}
	.getPhone::after {
		content: none;
	}
</style>


